package com.qyhl.module_home.home.fuquan;

import com.orhanobut.hawk.Hawk;
import com.qyhl.module_home.common.HomeUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.home.GatherBean;
import com.qyhl.webtv.commonlib.entity.home.HomeBean;
import com.qyhl.webtv.commonlib.entity.home.NewsBean;
import com.qyhl.webtv.commonlib.entity.live.ShoppingListBean;
import com.qyhl.webtv.commonlib.entity.news.AdvHomeBean;

import java.util.List;

public class FuQuanHomeModel implements FuQuanHomeContract.FuQuanHomeModel {
    private FuQuanHomeContract.FuQuanHomeModelPresenter presenter;

    public FuQuanHomeModel(FuQuanHomeContract.FuQuanHomeModelPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void getConfig() {
        EasyHttp.get(HomeUrl.GET_HOME)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .execute(new SimpleCallBack<HomeBean>() {

                    @Override
                    public void onError(ApiException e) {
                        presenter.setError(1, "出错啦，请稍后重试！");
                    }

                    @Override
                    public void onSuccess(HomeBean homeBean) {
                        if (homeBean != null) {
                            Hawk.put("home", homeBean);
                        }
                        presenter.setConfig(homeBean);
                    }
                });

    }

    @Override
    public void getAdvData() {

        EasyHttp.get(HomeUrl.GET_HOME_ADV)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .execute(new SimpleCallBack<List<AdvHomeBean>>() {

                    @Override
                    public void onError(ApiException e) {
                        presenter.setAdvBanner(false, null);
                    }

                    @Override
                    public void onSuccess(List<AdvHomeBean> advHomeBeans) {
                        if (advHomeBeans != null || advHomeBeans.size() > 0) {
                            Hawk.put("homeAdv", advHomeBeans);
                            presenter.setAdvBanner(true, advHomeBeans);
                        } else {
                            presenter.setAdvBanner(false, null);
                        }
                    }
                });

    }

    @Override
    public void getGatherData(final int page, final int lastNewsId, String type) {

        String sectionId = CommonUtils.getInstance().getSectionId();

        String tag = CommonUtils.getInstance().getSiteName();
        String sitr = String.valueOf(CommonUtils.getInstance().getSite());

        EasyHttp.get(HomeUrl.GET_GATHER_NEWS)
                .params("siteId", sitr)
                .params("tagName", tag)
                .params("ID", String.valueOf(lastNewsId))
                .params("page", String.valueOf(page))
                .params("sectionId", sectionId)
                .params("type", type)
                .execute(new SimpleCallBack<List<GatherBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (page > 1) {
                            presenter.setError(3, "网络异常，加载失败！");
                        } else {
                            presenter.setError(2, "网络异常，加载失败！");
                        }
                    }

                    @Override
                    public void onSuccess(List<GatherBean> gatherBeans) {
                        if (gatherBeans != null && gatherBeans.size() > 0) {
                            if (lastNewsId == 0 && page == 1) {
                                Hawk.put("gather", gatherBeans);
                            }
                            if (page > 1) {
                                presenter.setGatherNews(gatherBeans, true);//加载更多
                            } else {
                                presenter.setGatherNews(gatherBeans, false);//刷新
                            }
                        } else {
                            presenter.setError(5, "数据获取失败！");
                        }
                    }
                });

    }

    @Override
    public void getTopShop() {
        EasyHttp.get(HomeUrl.GET_SHOPPING_TOP)
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .execute(new SimpleCallBack<List<ShoppingListBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        presenter.setTopShop(null);
                    }

                    @Override
                    public void onSuccess(List<ShoppingListBean> shoppingListBean) {
                        presenter.setTopShop(shoppingListBean);
                    }
                });
    }

    @Override
    public void getTodayRec(int id) {
        EasyHttp.get(HomeUrl.GET_HOME_NEWS)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("InnerCode", id+"")
                .params("ID", "0")
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .execute(new SimpleCallBack<List<NewsBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        presenter.setTodayRec(null);
                    }

                    @Override
                    public void onSuccess(List<NewsBean> newsBeans) {
                            if (newsBeans != null && newsBeans.size() > 0) {
                                presenter.setTodayRec(newsBeans);
                            } else {
                                presenter.setTodayRec(null);
                            }
                    }
                });

    }
}
